package com.tools.monitor.redis;

import cn.hutool.core.util.StrUtil;

import java.util.ArrayList;
import java.util.Properties;

/**
 * @Author : ShangJY
 * @create 2024/6/15 22:54
 */
public class RedisConvert {

    public static RedisMonitorVo build(Properties info, Long dbSize, Properties commandStats) {
        RedisMonitorVo respVO = RedisMonitorVo.builder()
                .info(info)
                .dbSize(dbSize)
                .commandStats(new ArrayList<>(commandStats.size())).build();
        commandStats.forEach((key, value) -> {
            respVO.getCommandStats().add(RedisMonitorVo.CommandStat.builder()
                    .command(StrUtil.subAfter((String) key, "cmdstat_", false))
                    .calls(Long.valueOf(StrUtil.subBetween((String) value, "calls=", ",")))
                    .usec(Long.valueOf(StrUtil.subBetween((String) value, "usec=", ",")))
                    .build());
        });
        return respVO;
    }
}
